package us.icitap.timsportal.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import us.icitap.timsportal.entity.Module;
import us.icitap.timsportal.entity.RoleEnum;
import us.icitap.timsportal.management.ModuleEditor;
import us.icitap.timsportal.management.PasswordEditor;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.web.TimsPortalApp;
import us.icitap.timsportal.web.custom.ModuleButton;
import us.icitap.timsportal.web.custom.PortalLabel;
import us.icitap.timsportal.web.custom.PortalVerticalLayout;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;

@SuppressWarnings("serial")
public class ConfigMenu extends PortalVerticalLayout implements Button.ClickListener{
	
	private static final ThemeResource ICON = new ThemeResource("./icons/Categories-applications-system-icon.png");
	
	private Map<String, Button> buttonsMap = new HashMap<String, Button>();
	private ModuleButton usersButton = new ModuleButton(TimsPortalLabels.getLabel("label.users"));
	private ModuleButton modulesButton = new ModuleButton(TimsPortalLabels.getLabel("label.modules"));
	private ModuleButton passwordButton = new ModuleButton("Ndrysho fjalekalimin");
	private TimsPortalApp app;
	
	public ConfigMenu(){
		setMargin(true, false, true, false);
		setSizeFull();
		PortalLabel title = new PortalLabel(TimsPortalLabels.getLabel("title.managamentBold"), Label.CONTENT_XHTML);
		title.setSizeFull();
		title.setStyleName("menu");
		usersButton.setIcon(ICON);
		usersButton.addListener(this);
		usersButton.setImmediate(true);
		usersButton.setEnabled(false);
		
		modulesButton.setIcon(ICON);
		modulesButton.addListener(this);
		modulesButton.setImmediate(true);
		modulesButton.setEnabled(false);
		
		passwordButton.setIcon(ICON);
		passwordButton.addListener(this);
		passwordButton.setImmediate(true);
//		passwordButton.setEnabled(false);
		
		
		addComponent(title);		
		addComponent(usersButton);
		addComponent(modulesButton);
		addComponent(passwordButton);
		
	}
	
	@Override
	public void attach(){
		super.attach();
		this.app = getApplication();

		usersButton.setEnabled(getApplication().getSessionHandler().getUser().getRoles()
				.contains(getApplication().getPortalBean().getRole(RoleEnum.MANAXHIM_PERDORUESISH.name())));
		modulesButton.setEnabled(getApplication().getSessionHandler().getUser().getRoles()
				.contains(getApplication().getPortalBean().getRole(RoleEnum.MANAXHIM_PERDORUESISH.name())));
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// TODO Auto-generated method stub
		if(event.getButton()==usersButton){
			app.setMainComponent(app.getUserSearch());
		} else if(event.getButton()==modulesButton){
			List<Module> list = app.getPortalBean().getListModule(true);
			ModuleEditor mEditor = new ModuleEditor(list);
			app.setMainComponent(mEditor);
		} else if(event.getButton()==passwordButton){
			PasswordEditor pel = new PasswordEditor();
			app.setMainComponent(pel);
		}
		
	}
	
}
